package com.friend.campusnotice;

import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import mabbas007.tagsedittext.TagsEditText;

public class SendMSGActivity extends AppCompatActivity implements TagsEditText.TagsEditListener {

    private TagsEditText mTagsEditText;
    private ImageView send_my_msg;
    private EditText about_to_send_msg;
    private TextView addMore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_msg);

        initView();


    }

    private void initView() {
        mTagsEditText = (TagsEditText) findViewById(R.id.tagsEditText);
        send_my_msg = (ImageView) findViewById(R.id.send_my_msg);
        about_to_send_msg = (EditText) findViewById(R.id.about_to_send_msg);

        send_my_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] numbers = (mTagsEditText.getText() + "").split(" ");
                if (numbers.length > 0) {
                    sendmsg(numbers, about_to_send_msg.getText() + "");
                }


            }
        });

        addMore = (TextView) findViewById(R.id.add_more);
        addMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SendMSGActivity.this, PcikContactsActivity.class);
                startActivityForResult(intent, 1);
            }
        });

        List<String> numberList = new ArrayList<String>();
//        numberList.add("10086");
//        numberList.add("10010");
//        numberList.add("13005928409");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, numberList);
        mTagsEditText.setAdapter(adapter);
        mTagsEditText.setThreshold(1);
        // mTagsEditText.setTags();

        mTagsEditText.setTagsListener(this);
        mTagsEditText.setTagsWithSpacesEnabled(true);
        // Log.i("xiao",((mTagsEditText.getText()+"").split(" "))[0]);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.i("xiaohei", resultCode + "  resultCode");
        if (resultCode == 2) {


            ArrayList<String> item = data.getStringArrayListExtra("items");
            String[] selectedItems = new String[item.size()];

            for (int i = 0; i < item.size(); i++) {
                Log.i("xiaohei", item.get(i));
                selectedItems[i] = item.get(i);
            }

            mTagsEditText.setTags(selectedItems);
        }

//        switch (requestCode) {
//            case 1:
//                if (resultCode == RESULT_OK) {
//                    Uri contactData = data.getData();
//                    Cursor cursor = managedQuery(contactData, null, null, null,
//                            null);
//                    cursor.moveToFirst();
//                    String num = this.getContactPhone(cursor);
//
//                }
//                break;
//
//            default:
//                break;
//        }


    }

    private void sendmsg(String[] phone, String message) {

        //   PendingIntent pi = PendingIntent.getActivity(this, 0, null, 0);
        if (phone.length < 1) {
                return;
        }
        SmsManager sms = SmsManager.getDefault();

        for (int i = 0; i < phone.length; i++) {
            sms.sendTextMessage(phone[i], null, message, null, null);
        }


        new SweetAlertDialog(SendMSGActivity.this, SweetAlertDialog.SUCCESS_TYPE)
                .setTitleText("发送成功！")
                .setConfirmText("确定")
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismissWithAnimation();
                        finish();
                    }
                })
                .show();

    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            mTagsEditText.showDropDown();
        }
    }

    @Override
    public void onTagsChanged(Collection<String> collection) {
        Log.d("xiaohei", "Tags changed: ");
        Log.d("xiaohei", Arrays.toString(collection.toArray()));
    }

    @Override
    public void onEditingFinished() {

    }
}
